package com.naiterui.ehp.bs.cms.modules.user.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.naiterui.ehp.bp.bo.patient.PatientInquirerBO;
import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.security.annotation.SysLog;
import com.naiterui.ehp.bp.support.advice.IgnoreResponseEntityWrap;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bs.cms.common.controller.AbstractController;
import com.naiterui.ehp.bs.cms.common.vo.PageParamsVO;
import com.naiterui.ehp.bs.cms.common.vo.PageVO;
import com.naiterui.ehp.bs.cms.modules.user.feign.EmrFeignClient;
import com.naiterui.ehp.bs.cms.modules.user.feign.PatientFeignClient;
import com.naiterui.ehp.bs.cms.modules.user.service.IPatientService;
import com.naiterui.ehp.bs.cms.modules.user.vo.CaseDetailVO;
import com.naiterui.ehp.bs.cms.modules.user.vo.PatientDetailsVO;
import com.naiterui.ehp.bs.cms.modules.user.vo.PatientPageVO;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

@Api(tags = {SwaggerTag.CMS + SwaggerTag.USER})
@RestController
@RequestMapping("/user/patient")
public class PatientController extends AbstractController {

    @Autowired
    private IPatientService patientService;
    @Autowired
    private EmrFeignClient emrFeignClient;
    @Autowired
    private PatientFeignClient patientFeignClient;

    /**
     * 列表
     *
     * @return
     */
    @ApiOperation(value = "患者列表")
    @ResponseBody
    @SysLog("患者列表")
    @GetMapping("/list")
    @PreAuthorize("@p.check('user:patient:list')")
    public PageVO<PatientPageVO> list(PageParamsVO pageParamsVO, PatientPageVO patientPageVO) {
        return this.patientService.queryPage(pageParamsVO, patientPageVO);
    }

    @ApiOperation(value = "患者详情")
    @ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "long", paramType = "path")
    @SysLog("患者详情")
    @GetMapping("/{patientId}")
    @PreAuthorize("@p.check('user:patient:list')")
    public PatientDetailsVO get(@PathVariable("patientId") Long patientId) {
        return patientService.getPatientDetailsVO(patientId);
    }

    @ApiOperation(value = "病例列表")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "long", paramType = "query"),
        @ApiImplicitParam(name = "inquirerId", value = "就诊人ID", required = true, dataType = "long",
            paramType = "query")})
    @SysLog("病例列表")
    @GetMapping("/case")
    @IgnoreResponseEntityWrap
    @PreAuthorize("@p.check('user:patient:list')")
    public String caseList(Long patientId, Long inquirerId, PageParamsVO pageParamsVO) throws BusinessException {
        return emrFeignClient.patientCaseList(patientId, inquirerId, pageParamsVO.getPageNo(), pageParamsVO.getPageSize());
    }

    @ApiOperation(value = "就诊人员")
    @ApiImplicitParam(name = "patientId", value = "患者ID", required = true, dataType = "long", paramType = "query")
    @SysLog("就诊人成员列表")
    @GetMapping("inquirer/list")
    @PreAuthorize("@p.check('user:patient:list')")
    public List<PatientInquirerBO> inquirerList(Long patientId) throws BusinessException {
        return patientFeignClient.patientInquirerList(patientId);
    }

    /**
     * 病历下载、打印操作上报
     *
     * @throws BusinessException
     */
    @ResponseBody
    @SysLog("病历下载、打印操作上报")
    @GetMapping("/case/download/report")
    @ApiOperation("病历下载、打印操作上报")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId", value = "病例ID", dataType = "string", required = true),
            @ApiImplicitParam(name = "type", value = "操作类型 1:下载，2:打印", dataType = "int", required = true)
    })
    public void caseDownloadReport(String recordId, Integer type) throws BusinessException {
        patientService.caseDownloadReport(recordId, type);
    }

    /**
     * 病历详情信息
     *
     * @param recordId
     *
     * @return
     *
     * @throws BusinessException
     */
    @ResponseBody
    @SysLog("病历详情")
    @GetMapping("/case/{recordId}")
    @ApiOperation("病历详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "recordId", value = "病例ID", dataType = "long", required = true)
    })
    public CaseDetailVO caseDetail(@PathVariable("recordId") String recordId, PageParamsVO pageParamsVO) throws BusinessException {
        return this.patientService.caseDetail(pageParamsVO, recordId);
    }
}
